var map;
var markersArray = [];
var bounds = new google.maps.LatLngBounds ();
var geocoder;
var infoWindow = new google.maps.InfoWindow();
var markerClusterer = null;
  
function refreshMap() {
	if (markerClusterer) {
		markerClusterer.clearMarkers();
	}
	markerClusterer = new MarkerClusterer(map, markersArray, {
		maxZoom: 18,
	  	gridSize: null,
	  	styles: null
	});
}

function clearClusters(e) {
	e.preventDefault();
	e.stopPropagation();
	markerClusterer.clearMarkers();
}


function initialize(currentLocation,element) {
  geocoder = new google.maps.Geocoder();
  var myLatlng = currentLocation;
  var myOptions = {
    zoom: 13,
	maxZoom: 18,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  map = new google.maps.Map(document.getElementById(element), myOptions);
}
  
function codeAddress(address) {
	geocoder.geocode( { 'address': address}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			var tempLatLng = results[0].geometry.location;
			map.setCenter(tempLatLng);
  		} else {
			alert("Không thể tìm thấy địa chỉ bạn nhập vào.");
  		}
	});
}
  
function placeMarker(location,image,title,content) {
	var marker = new google.maps.Marker({
		position: location, 
		icon:image,
		map: map
	});
	google.maps.event.addListener(marker, "click", function(){
		infoWindow.setOptions({
			position: location,
			title: title,
			content: content
		});
		infoWindow.open(map, marker);
	
	});
	markersArray.push(marker);
	bounds.extend (location);
}

google.maps.event.addListener(map, "click", function(){
	infoWindow.close();
});

function deleteMarkers() {
  if (markersArray) {
    for (i in markersArray) {
      markersArray[i].setMap(null);
    }
    markersArray.length = 0;
  }
}

function search_map()
{
	var address  = $("#txt_search_map_value").val();
	codeAddress(address);
	return false;
}